<?php

namespace app\admin\controller\ykjp\sell;

use app\common\controller\Backend;

/**
 * 采购录单
 *
 * @icon fa fa-circle-o
 */
class Sell extends Backend {

    protected $model = null;
    protected $distinguish = true;
    protected $searchFields = "name";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\sell\Sell;
    }

    /**
     * 查看
     */
    public function index() {
        $date = date("Y-m-d", time());
        $this->assign("date", $date);
        if ($this->request->isAjax()) {
            $a = $this->request->get('a');
            if ($a == 1) {
                //点击获取客户信息
                $this->model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $total = $this->model
                        ->where($where)
                        ->where('firmid', $this->auth->firmid)
                        ->order($sort, $order)
                        // ->field("name,contact,phone,description,id")
                        ->count();
                $list = $this->model
                        ->where($where)
                        ->where('firmid', $this->auth->firmid)
                        ->order($sort, $order)
                        ->limit($offset, $limit)
                        ->field("name,contact,phone,description,id,status")
                        ->select();
                $list = collection($list)->toArray();
                $result = array("total" => $total, "rows" => $list);
                return json($result);
            } else {
                //点击获取商品信息
                $this->model = new \app\admin\model\ykjp\product\Product;
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $total = $this->model
                        ->where($where)
                        ->where('inventory', '>', 0)
                        ->where('firmid', $this->auth->firmid)
                        ->order($sort, $order)
                        ->count();
                $list = $this->model
                        ->where($where)
                        ->where('inventory', '>', 0)
                        ->where('firmid', $this->auth->firmid)
                        ->order($sort, $order)
                        ->limit($offset, $limit)
                        ->select();
                $result = array("total" => $total, "rows" => $list);
                return json($result);
            }
        }
        return $this->view->fetch();
    }

    /**
     * 获取客户详情
     */
    public function getCustomerByid($ids) {
        $this->model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
        $row = $this->model->get(['id' => $ids, "firmid" => $this->auth->firmid]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isAjax()) {
            $city = "";
            if ($row) {
                $row = $row->toArray();
                if (isset($row['city']) && $row['city'] != "") {
                    $city = explode('/', $row['city']);
                }
            }
            $this->success(null, null, ['row' => $row, "city" => $city]);
        }
        $this->error(__('No Results were found'));
    }

    /**
     * 获取产品详情
     */
    public function getProductByid($ids) {
        $this->model = new \app\admin\model\ykjp\product\Product;
        $row = $this->model->get(['id' => $ids, "firmid" => $this->auth->firmid]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isAjax()) {
            $city = "";
            if ($row) {
                $row = $row->toArray();
            }
            $this->success(null, null, ['row' => $row]);
        }
        $this->error(__('No Results were found'));
    }

    /**
     * 销售录单
     */
    public function addSell() {
        if ($this->request->isAjax()) {
            //销售录单中客户信息数组
            $row = $this->request->param("row/a");
            //销售录单中商品信息数组
            $product = $this->request->param("product/a");
            $all = json_decode($product[0]);
            $tep = $this->model->object_array($all);

            //判断是否选择客户
            if (!isset($row['customer_info_id']) || $product == "" || !isset($product[0])) {
                $this->error(__('No Results were found'));
            }
            //获取客户信息
            $customerModel = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
            $customer = $customerModel->get(['id' => $row['customer_info_id'], "firmid" => $this->auth->firmid]);
            if (!$customer) {
                $this->error(__('No Results were found'));
            }

            //赋值参数
            $row['createtime'] = time();
            $row['updatetime'] = time();
            $row['admin_id'] = $this->auth->id;  //录单人员
            $row['code'] = (isset($row['code']) && $row['code'] != "") ? $row['code'] : "YK" . date("ymdHis", time()) . rand(1000, 9999);
            $row['delivery_time'] = strtotime($row['delivery_time']);
            $row['billing_time'] = strtotime($row['billing_time']);
            $row['firmid'] = $this->auth->firmid;
            $row['status_list'] = 1;  //订单状态默认设置为待审核
            //插入销售订单主表 并获取条目ID
            $sellModel = new \app\admin\model\ykjp\sell\Sell;
            $isnter = $sellModel->insertGetId($row);

            if ($isnter) {
                //设置销售订单副表 要插入的数据
                $arr = array();
                foreach ($tep as $value) {
                    $value["sell_id"] = $isnter;
                    $value["createtime"] = time();
                    $value["updatetime"] = time();
                    $value['firmid'] = $this->auth->firmid;
                    $arr[] = $value;
                }
                //初始化销售订单副表模型
                $sellProduct = new \app\admin\model\ykjp\sell\Product;
                $resul = $sellProduct->insertAll($arr);
                if ($resul) {
                    $this->success(null, null, ['result' => "success"]);
                } else {
                    $sellModel->where("id", $isnter)->delete();
                    $this->error(__('fail'));
                }
            }
        }
        $this->error(__('Illegal operation'));
    }

}
